var merchantNo;
var selectOption;
$(function () {
	
	var thisURL = document.URL;   
	var getval =thisURL.split('?')[1];  
	merchantNo= getval.split("=")[1];
	$('#merchantNo').val(merchantNo);
    $("#jqGrid").jqGrid({
        url: '../payChannel/list',
        datatype: "json",
        postData:{
        	merchantNo:merchantNo
        	},
        colModel: [			
            { label: '开户行名称', name: 'bankName',width:280, index: 'bankId' ,
				formatter: function(value, options, row){
					if(value != ''){
						return row.bankName+'/'+row.bankUser+'/'+row.bankCard;
					}else {
						return '';
					}
			}},       
			{ label: '企业名称', name: 'shopName',width:100, index: 'm.shopname' }, 
            { label: '开户行名称', name: 'bankId' ,hidden:true},   
            { label: '编号', name: 'payChannelId', index: 'merchant_id' ,hidden:true}, 
			//{ label: '商户号', name: 'merchantNo', index: 'merchant_no', width: 100 }, 
			{ label: '支付商编号', name: 'payChannelNo', index: 'paychannel_no' }, 
			{ label: '支付渠道类型', name: 'payType',width:100, index: 'pay_type' ,
				formatter: function(value, options, row){
					if(value == 1){
						return '微信';
					}else if(value == 2){
						return '支付宝';
					}else {
						return '';
					}
				}	}, 		
			{ label: '费率(%)', name: 'payRate',width:80, index: 'pay_rate' }, 			
			{ label: '密钥', name: 'payKey',width:300, index: 'pay_key' }, 	
			{ label: '最大限额（元/笔）', name: 'maxTransactionMoney', index: 'max_transaction_money' }, 	
			{ label: '日累计限额（元）', name: 'dayMaxMoney', index: 'day_max_money' }, 	
			{ label: '创建时间', name: 'createTime', index: 'pay_key'},
			{ label: '是否支持信用卡', name: 'iscreditCard',width:110, index: 'iscreditCard' ,
				formatter: function(value, options, row){
					if(value == 0){
						return '否';
					}else if(value == 1){
						return '是';
					}else {
						return '';
					}
				}	}
        ],
		viewrecords: true,
        height: 'auto',
        rowNum: 10,
        //width:1200,
		rowList : [10,20,30],
        rownumbers: false, 
        rownumWidth: 5, 
        multiselect: true,
        autowidth:true,
        shrinkToFit:false,//显示底部水平滚动条
        autoScroll: true,//显示底部水平滚动条
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            id: "payChannelId", 
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
  //根据商户号获取银行信息下拉
    $.ajax({
    	type: "POST",
	    url: "../bankInfo/getBankInfoSelect",
	    data: JSON.stringify(merchantNo),
	    success: function(result){
	    	//console.log(result.options);
	    	var list = result.options;
	    	for(var i=0;i<list.length;i++){
	    		var obj = {};
	    		obj = list[i];
	    		vm.bankOptions[i] = obj;
	    	}
	    	//console.log(vm.payTypeOptions);
	    	//vm.bankOptions[0] = {text:"否",value:"0"};
	    	//console.log(vm.bankOptions);
		}
	});
    $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });  
});


var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			payChannelId: null,
		},
		showList: true,
		title: null,
		payChannel: {},
		selected: '',
		payTypeOptions: [
	      { text: '微信', value: '1' },
	      { text: '支付宝', value: '2' }
	    ],
	    iscreditCardoptions:[{text:"否",value:"0"},
	    	{text:"是",value:"1"}],
	    bankOptions:[],
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.payChannel = {
				merchantNo : merchantNo
			};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
			var url = vm.payChannel.payChannelId == null ? "../payChannel/save" : "../payChannel/update";
			$("#submitBtn").attr("disabled","disabled");
			if(vm.payChannel.payType == null || "" == vm.payChannel.payType){
				alert('请选择一个支付渠道类型');
				return;
			}
			
			if(vm.payChannel.payChannelNo == null || "" == vm.payChannel.payType){
				alert('支付渠道编号不能为空');
				return;
			}
			if(vm.payChannel.payType == null || "" == vm.payChannel.payType){
				alert('支付渠道类型不能为空');
				return;
			}
			if(vm.payChannel.payKey == null || "" == vm.payChannel.payType){
				alert('支付渠道密钥不能为空');
				return;
			}
			var payRate = vm.payChannel.payRate;
			//有两位小数的正实数：^[0-9]+(.[0-9]{2})?$
			if(payRate == null  || "" == payRate){
				alert('支付渠道费率不能为空');
				return;
			}else if(!(/^[0-9]+(.[0-9]{2})?$/.test(payRate))) {
	             alert("费率只能填写两位小数的正实数");
	             return ;
			}
			var maxTransactionMoney = vm.payChannel.maxTransactionMoney;
			//非零的正整数：^[1-9]\d*$
			if(maxTransactionMoney == null || "" == maxTransactionMoney){
				alert('支付渠道最大限额不能为空');
				return;
			}else if (!(/^[1-9]\d*$/.test(maxTransactionMoney))) {
	             alert("最大限额只能填写非零的正整数");
	             return ;
			}
			var dayMaxMoney = vm.payChannel.dayMaxMoney;
			if(dayMaxMoney == null || "" == dayMaxMoney){
				alert('日累计限额不能为空');
				return;
			}else if (!(/^[1-9]\d*$/.test(dayMaxMoney))) {
	             alert("日累计限额只能填写非零的正整数");
	             return ;
			}
			if(vm.payChannel.iscreditCard == null || "" ==  vm.payChannel.payType){
				alert('是否支持信用卡不能为空');
				return;
			}
			
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.payChannel),
			    success: function(r){
			    	if(r.code == 0){
						alert(r.msg, function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
			    	$("#submitBtn").removeAttr("disabled");
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../payChannel/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			//console.log(id)
			$.get("../payChannel/info/"+id, function(r){
                vm.payChannel = r.payChannel;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			page = 1;
			var keyword = $("#keyword").val();
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{keyword:keyword},
            }).trigger("reloadGrid");
		},
	}
});